package com.senthink.gl.business.userlog.business;

import com.senthink.gl.business.device.people.SwitchEnum;
import com.senthink.gl.business.userlog.enums.LogControlTypeEnum;
import com.senthink.gl.business.userlog.enums.LogDetailEnum;
import com.senthink.gl.business.userlog.enums.LogOperateEnum;
import com.senthink.gl.business.userlog.SystemUserLog;
import com.senthink.gl.business.domain.entity.UserLog;
import com.senthink.gl.business.device.CmdRequest;
import com.senthink.gl.business.domain.entity.*;
import com.senthink.gl.business.domain.service.*;
import com.senthink.gl.business.fault.DeviceTypeEnum;
import com.senthink.gl.business.messages.CodeMsg;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 设备相关日志操作记录类
 *
 * @author yuanpeng@senthink.com
 * @data 2018-02-01 15:29
 */

@Component
public class LogDeviceBusiness {

    @Autowired
    private CodeMsg codeMsg;
    @Autowired
    private AreaDataService areaDataService;

    public void main(SystemUserLog systemUserLog, UserLog userLog, JoinPoint joinPoint) {
        //批量导入设备
        if (systemUserLog.detail() == LogDetailEnum.IMPORT_DEVICE) {
            userLog.setDetails(systemUserLog.detail().getDetail() + systemUserLog.deviceType().getDeviceName());
        } else if (systemUserLog.operate() == LogOperateEnum.CONTROL) {
            //设备控制日志记录
            if (systemUserLog.detail() == LogDetailEnum.SINGLE_CONTROL) {
                singleControl(systemUserLog, userLog, joinPoint);
            } else if (systemUserLog.detail() == LogDetailEnum.BATCH_CONTROL) {
                if (systemUserLog.deviceType() == DeviceTypeEnum.LAMP) {
                    //batchLampControl(systemUserLog, userLog, joinPoint);
                } else {
                    batchControl(systemUserLog, userLog, joinPoint);
                }
            }
        } else if (systemUserLog.deviceType() == DeviceTypeEnum.SENSOR) {

        } else if (systemUserLog.deviceType() == DeviceTypeEnum.GEOMAGNETIC_RECEIVER && systemUserLog.operate() == LogOperateEnum.UPDATE) {
            if (systemUserLog.detail() == LogDetailEnum.BATCH_EDIT_LOCATION) {
                userLog.setDetails(systemUserLog.detail().getDetail() + ":" + systemUserLog.deviceType().getDeviceName());
            } else {
                Integer sn = (Integer) joinPoint.getArgs()[2];
                userLog.setDetails(systemUserLog.detail().getDetail() + ":" + systemUserLog.deviceType().getDeviceName() + "," + sn);
            }
        } else if (systemUserLog.detail() == LogDetailEnum.BATCH_EDIT_AREA) {
            userLog.setDetails(systemUserLog.detail().getDetail() + ":" + systemUserLog.deviceType().getDeviceName());
        } else if (systemUserLog.detail() == LogDetailEnum.BATCH_EDIT_LOCATION) {
            userLog.setDetails(systemUserLog.detail().getDetail() + ":" + systemUserLog.deviceType().getDeviceName());
        } else {
            //其他设备添加、编辑日志记录
            Object object = joinPoint.getArgs()[0];
            userLog.setDetails(systemUserLog.detail().getDetail() + ":" + systemUserLog.deviceType().getDeviceName() + "," + getSn(object.toString()));
        }
    }

    /**
     * 其他设备获取sn
     *
     * @param string
     * @return
     */
    private String getSn(String string) {
        int snIndex = string.indexOf("sn=");
        int snIndexEnd = string.indexOf(",", snIndex);
        return string.substring(snIndex + 3, snIndexEnd);
    }

    /**
     * 单个控制日志记录
     *
     * @param systemUserLog
     * @param userLog
     * @param joinPoint
     */
    private void singleControl(SystemUserLog systemUserLog, UserLog userLog, JoinPoint joinPoint) {
        String sn = "";
        if (systemUserLog.deviceType() == DeviceTypeEnum.LAMP) {
            //灯具控制
            if (systemUserLog.controlType() == LogControlTypeEnum.SWITCH) {
                //userLog.setDetails(systemUserLog.detail().getDetail() + ":" + systemUserLog.deviceType().getDeviceName() + SwitchEnum.fromCommand(lampCmdRequest.getCommand()).getName() + "," + lampCmdRequest.getSn());
            } else {
               // userLog.setDetails(systemUserLog.detail().getDetail() + ":" + systemUserLog.deviceType().getDeviceName() + systemUserLog.controlType().getType() + "," + lampCmdRequest.getSn());
            }
        } else {
            //LoopCmdRequest loopCmdRequest = (LoopCmdRequest) joinPoint.getArgs()[0];
            //userLog.setDetails(systemUserLog.detail().getDetail() + ":" + systemUserLog.deviceType().getDeviceName() + "," + loopCmdRequest.getCommandValue()+"," + loopCmdRequest.getLid());
        }
    }







    /**
     * 批量控制日志记录
     *
     * @param systemUserLog
     * @param userLog
     * @param joinPoint
     */
    private void batchControl(SystemUserLog systemUserLog, UserLog userLog, JoinPoint joinPoint) {
        CmdRequest cmdRequest = (CmdRequest) joinPoint.getArgs()[0];
        Area area = areaDataService.findByAid(cmdRequest.getAid())
                .orElse(null);
        userLog.setDetails(systemUserLog.detail().getDetail() + systemUserLog.deviceType().getDeviceName() +  ":" + (area == null ? "unknown" : area.getName()));
    }




}
